﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess.Repository;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using System.Text;

namespace ExaminationSys
{
    public partial class LearningDetail :  BasePage
    {
        public string LoginName { get; set; }

        private static readonly LoginLogService Proxy = ObjectFactory<LoginLogService>.Create();
        private static readonly TeacherInfoService ProxyTeacherInfo = ObjectFactory<TeacherInfoService>.Create();
        private static readonly SubSectionInfoService ProxySubSection = ObjectFactory<SubSectionInfoService>.Create();
        private static readonly SubSectionDetailInfoService ProxySubSectionDetailInfo = ObjectFactory<SubSectionDetailInfoService>.Create();

        public List<SubSectionInfo> SubSectionInfoCollection = new List<SubSectionInfo>();


        public List<SubSectionDetailInfo> GetListBySubSectionId(Guid subSectionid)
        {
            return ProxySubSectionDetailInfo.GetListBySubSectionId(subSectionid);
        }

        public SubSectionDetailInfo CurrentSelectItem=new SubSectionDetailInfo();

        //级别名称
        public string LevelName { get; set; }
        public int KnowlegdeLevel = 0;
        private Guid subDetailId = Guid.Empty;
        public Guid ID { get; set; }
        public string TeacherName { get; set; }
        public StringBuilder htmlStr = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            var levelTmp = Request["level"];
            var subDetailIdTmp = Request["id"];
            var sectionId = Request["sid"];

            KnowlegdeLevel = base.GetCurrentStudentLevel();
            //if(!int.TryParse(levelTmp, out KnowlegdeLevel))
            //{
            //    //if the level convert failed ,and then redirect to the 404 page or response the error message
            //    //获取知识点级别失败，转为初级开始或者直接跳转到404?
            //}

            if (!Guid.TryParse(subDetailIdTmp, out subDetailId))
            {
                //Redirect to the 404 page
                return;
            }

            if(!IsPostBack )
            {
                  base.ResetSession();
                var loginInfo = Session["StudentInfo"] as Student;
                if (loginInfo != null)
                {
                    LoginName = loginInfo.UserName;
                    if (KnowlegdeLevel == 0) KnowlegdeLevel = (int) EnumLevel.初级;
                    LevelName = Enum.GetName(typeof (EnumLevel), KnowlegdeLevel);
                    SubSectionInfoCollection = ProxySubSection.GetListByKnowlegdeLevel(KnowlegdeLevel, sectionId);
                   

                    CurrentSelectItem = ProxySubSectionDetailInfo.Get(subDetailId);

                    if(CurrentSelectItem==null)
                    {
                        //Redirect to the 404 page
                    }
                    var teacher = ProxyTeacherInfo.GetTeacherInfoByUserId(CurrentSelectItem.Opeator);

                    TeacherName = teacher == null ? "未知" : teacher.UserName;
                    ID = subDetailId;
                }
                else
                {
                    Response.Redirect("Index.aspx");
                }
               
            }
        }
    }
}